package com.zxc.controller.service;

import com.zxc.entity.SctService;
import com.zxc.service.CstCustomerService;
import com.zxc.service.CstTypeService;
import com.zxc.service.SctServiceService;
import com.zxc.service.SysUserService;
import com.zxc.utils.StringToDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/service")
public class CreateServiceController {

    @Autowired
    private SctServiceService sctServiceService;

    @Autowired
    private CstCustomerService cstCustomerService;

    @Autowired
    private CstTypeService cstTypeService;

    @Autowired
    private SysUserService sysUserService;

    @RequestMapping("/save")
    @ResponseBody
    public Map<String,Object> saveService(SctService sctService, String svrCreateDateStr){
        Map<String,Object> modelMap = new HashMap<>();
        //客户Id
        Long custNo = cstCustomerService.selectCstCustomerByCustName(sctService.getSvrCustName());
        sctService.setSvrCustNo(custNo);
        //服务类型
        String typeName = cstTypeService.getCstTypeNameById(sctService.getSvrType());
        sctService.setSvrType(typeName);
        //创建人
        String custCreate = sysUserService.selectSysUserBySysUserId(sctService.getSvrCreateBy());
        sctService.setSvrCreateId(Long.valueOf(sctService.getSvrCreateBy()));
        sctService.setSvrCreateBy(custCreate);

        //String to Date
        Date date = StringToDate.stringToDate(svrCreateDateStr);
        sctService.setSvrCreateDate(date);

        int result = sctServiceService.saveService(sctService);
        if (result > 0){
            modelMap.put("success",true);
        }else {
            modelMap.put("success",false);
        }
        return modelMap;
    }


}
